package com.atguigu.lease.web.admin.task;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: 刘安杰
 * @Date: 2024/8/16
 * @Time: 22:26
 * @Description:
 */
@Component
public class UpdateLeaseAgreementStatusTask {

    @Autowired
    private LeaseAgreementService LeaseAgreementService;

    //每天零点零分零秒检查租约日期是否到期
    @Scheduled(cron = "0 50 22 * * ?")
    public void updateLeaseAgreementStatus() {
        //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<LeaseAgreement>();
        //封装查询条件
        //1.租约结束日期小于等于当前日期
        leaseAgreementLambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        //2.租约状态为2,5,7
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //设置更新的字段
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        //调用带条件的更新
        LeaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }

}
